<template>
  <div class="wrap">
    <div class="show-title">
      <span>播放任务</span>
    </div>
    <!-- 创建任务 -->
    <el-form
      :inline="true"
      :model="ruleform"
      ref="from"
      :rules="rules"
      class="demo-form-inline form-wrap"
    >
      <el-form-item label="任务名称" prop="task_name">
        <el-input
          v-model="ruleform.task_name"
          message
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="循环间隔时间" prop="loop_interval">
        <el-input
          v-model.number="ruleform.loop_interval"
          message
          placeholder
        ></el-input>
      </el-form-item>
      <span class="second">秒</span>
      <el-form-item>
        <el-button type="primary" plain @click="onSubmit('from')"
          >创建</el-button
        >
        <el-button type="warning" plain @click="onReset('from')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table
      class="form-table"
      ref="table"
      :data="tableData"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
    >
      <!-- <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
      ></el-table-column> -->
      <el-table-column
        prop="task_id"
        label="任务标识"
        min-width="160"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.task_id
        }}</template>
      </el-table-column>

      <el-table-column
        prop="task_name"
        label="任务名称"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">{{
         scope.row.task_name
        }}</template>
      </el-table-column>
      <el-table-column
        prop="user_name"
        label="管理员"
        min-width="60"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.user_name
        }}</template>
      </el-table-column>

      <el-table-column
        prop="loop_interval"
        label="循环间隔（秒）"
        min-width="80"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.loop_interval
        }}</template>
      </el-table-column>
      <!-- <el-table-column
        prop="status"
        label="任务状态"
        min-width="60"
        align="center"
        sortable
      >
        <template slot-scope="scope">{{
          scope.row.status==0?"关闭":"开启"
        }}</template>
      </el-table-column> -->
      <el-table-column
        prop="created_time"
        label="创建时间"
        min-width="100"
        align="center"
        sortable
      >
        <template slot-scope="scope">{{
          scope.row.created_time
        }}</template>
      </el-table-column>
      <el-table-column
        class="play-box"
        label="操作"
        min-width="180"
        align="center"
        :key="Math.random()"
        v-if="permissionData.hideActionArr.length < 4"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="success"
            @click="editFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['video/live/playing/looptask/device/set'],
              index: scope.$index,
              key: 'hideAction',
              id: 'edit',
            }"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="primary"
            @click="playFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['video/live/playing/looptask/device/list'],
              index: scope.$index,
              key: 'hideAction',
              id: 'play',
            }"
            >播放</el-button
          >
          <el-button
            size="small"
            type="warning"
            @click="modifyFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['video/live/playing/looptask/update'],
              index: scope.$index,
              key: 'hideAction',
              id: 'modify',
            }"
            >修改</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="delFn(scope.$index, scope.row)"
            v-permissions="{
              url: ['video/live/playing/looptask/delete'],
              index: scope.$index,
              key: 'hideAction',
              id: 'del',
            }"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="pageSizesArr"
      :page-size="pagesize"
      :total="total"
      :current-page="currpage"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <!-- 点击修改，弹出框 -->
    <Modify
      v-if="dialogVisible_modify.show"
      :dialogVisible_modify="dialogVisible_modify"
      :task_id="task_id_check"
      :task_name="task_name"
      :loop_interval="loop_interval"
      @refreshTableList="refreshTableList"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import Modify from "./Modify";
export default {
  name: "palyTask",
  components: { Modify },
  props: {},
  data() {
    var checkNumber = (rule, value, callback) => {
      if (value != "") {
        if (/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value) == false) {
          callback(new Error("请输入大于0的数字"));
        } else {
          callback();
        }
      } else {
        callback();
      }
      callback();
    };
    return {
      currpage: 1, //当前页
      pagesize: 10, //每页几条
      pageSizesArr: globalPageSizes, //每页显示个数选择器的选项设置[5,20,50,100]
      total: 0, // 总数
      tableData: [], // 表格数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
      ruleform: {
        task_name: "",
        loop_interval: 60,
      },
      rules: {
        task_name: [{ required: true, message: "请输入任务名称", trigger: "blur" }],
        loop_interval: [
          { required: true, message: "请输入时间" },
          { validator: checkNumber, trigger: "blur" },
        ],
      },
      //修改弹出框
      dialogVisible_modify: {
        show: false,
      },
      task_id_check: "", //打开弹窗，当前行task_id
      loop_interval: 60,
      permissionData: {
        hideActionArr: [], // 注意这里数组命名，hideAction对应你的key，后面加上Arr【必须这样命名，否则指令获取不到数组】
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.initList();
  },
  methods: {
    //初始列表
    initList() {
      postData("/adminweb/video/live/playing/looptask/list", {
        page: 1,
        page_num: 10,
      })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            if (!res.datas || !res.datas.tasks) {
              return;
            }
            let tasks = res.datas.tasks;
            this.tableData = tasks;
            this.total = res.datas.total_count;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //创建任务
    onSubmit(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
          if (that.ruleform.task_name == ""||that.ruleform.loop_interval == "") {
            // that.$message.error("必输项未填写，无法提交");
            return;
          }
        if (valid) {
          postData("/adminweb/video/live/playing/looptask/add", {
            task_name: that.ruleform.task_name,
            loop_interval: that.ruleform.loop_interval,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("创建成功！");
                that.ruleform.task_name = "";
                that.initList();
              } else {
                that.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch((err) => {
              that.$message.error(res.code + "，" + res.msg);
            });
        } else {
          
        }
      });
    },
    // 重置
    onReset(val) {
      this.ruleform.task_name = "";
      this.ruleform.loop_interval = "";
      //this.$refs[val].resetFields();
    },
    //编辑
    editFn(index, val) {
      let task_id = val.task_id;
      let task_name = val.task_name;
      let loop_interval = val.loop_interval;
      this.$router.push({
        path: "/playTask/editTask",
        query: {
          task_id: task_id,
          task_name: task_name,
          loop_interval: loop_interval,
        },
      });
    },
    //播放
    playFn(index, val) {
      let task_id = val.task_id;
      let task_name = val.task_name;
      let loop_interval = val.loop_interval;
      //   console.log(loop_interval);
      this.$router.push({
        path: "/playTask/playAuto",
        query: {
          task_id: task_id,
          task_name: task_name,
          loop_interval: loop_interval,
        },
      });
    },
    //点击修改，弹窗
    modifyFn(index, row) {
      //console.log(row);
      this.dialogVisible_modify = { show: true };
      this.task_id_check = row.task_id;
      this.task_name = row.task_name;
      this.loop_interval = row.loop_interval;
    },
    //刷新列表
    refreshTableList() {
        this.dialogVisible_modify = { show: false };
      this.initList();
    },
    //删除
    delFn(index, rows) {
      var that = this;
      that
        .$confirm("此操作将删除:" + rows.task_name + ", 是否继续?", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          postData("/adminweb/video/live/playing/looptask/delete", {
            task_id: rows.task_id,
          })
            .then((res) => {
              //console.log(res);
              if (res.code == "0") {
                // that.$message({
                //   type: "success",
                //   message: "删除成功!",
                // });
                that.$message.success("删除成功!");
                that.initList();
              } else {
                that.$message.error(res.code + ":" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error("创建失败");
            });
        })
        .catch((res) => {
          that.$message.success("已取消");
        });
    },
    // 获取row的key值
    getRowKeys(row) {
      return row.org_id;
    },
    //点击,第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
      this.initList();
    },
    //每页有几条
    handleSizeChange(psize) {
      this.currpage = 1;
      this.pagesize = psize;
      this.initList();
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      // console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["task_id"]);
        }
      }
    },
    //排序
    changeSort(val) {
      // console.log(val);
    },
  },
};
</script>
<style scoped>
.form-wrap {
  padding-left: 20px;
}
.second {
  height: 40px;
  display: inline-block;
  line-height: 40px;
  font-size: 15px;
  color: #676a6d;
  margin-right: 30px;
}
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  background-color: #fff;
  padding: 15px;
  overflow: hidden;
}
.form-table {
  margin-top: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  background: #fff;
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}

/*title*/
.show-title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  position: relative;
  background: #c0dee2;
  padding-left: 15px;
  font-size: 14px;
  margin: 6px 0 20px 0;
  border-radius: 12px 12px 0 0;
  font-weight: bold;
}

.show-title::before {
  content: "";
  width: 2px;
  height: 16px;
  background: #676a6d;
  position: absolute;
  top: 12px;
  left: 27px;
}

.show-title span {
  float: left;
  color: #676a6d;
  margin-left: 25px;
}
/* title显示名称id */
.wrap >>> .table-wrap {
  border: none;
  box-shadow: none;
}
.wrap >>> .el-divider__text.is-center {
  font-size: 17px;
}
.wrap >>> .el-divider__text {
  background: #c0dee2;
  padding: 15px;
  border-radius: 8px;
}
.title-list {
  background: #fff;
  padding-left: 20px;
  height: 80px;
}
.title-list .txt {
  height: 10px;
}
</style>
